package hello;

import javax.websocket.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.HtmlUtils;

@Controller
public class GreetingController {

    @Autowired
    private SimpMessagingTemplate template;

    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Greeting greeting(HelloMessage message) throws Exception {
        Thread.sleep(1000); // simulated delay
        return new Greeting("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!");
    }
    @MessageMapping("/hello2")
    @SendToUser("/topic/greetings2")
    public Greeting greeting2(HelloMessage message) throws Exception {
        Thread.sleep(1000); // simulated delay
        return new Greeting("Hello2, " + HtmlUtils.htmlEscape(message.getName()) + "!");
    }
    @RequestMapping("/hello2")
    //@SendTo("/topic/greetings2")
    public void greeting2(HelloMessage message, Session session) throws Exception {
        Thread.sleep(1000); // simulated delay
        String text = "Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!";
        // session.getBasicRemote().sendText(text);
        // System.out.println(text);
//        template.convertAndSend(destination, text);
    }

}
